package com.group4.DB;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.group4.DAO.ForumDAO;
import com.group4.DAO.PostDAO;

public class Post {

	private int ID;
	private int postOwner;
	private String postTopic;
	private Date postDateTime;
	private int postVoteCount;
	private String postText;
	private int postVideoId;
	private int postForumId;

	static PreparedStatement ps = null;
	static ResultSet rs = null;

	public static PostDAO getPostWithID(int ID) {

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return null;
		}

		PostDAO post = null;

		try {
			ps = ConnectionManager.conn
					.prepareStatement("select * from tbl_Post where ID = ?");

			ps.setInt(1, ID);

			rs = ps.executeQuery();

			while (rs.next()) {

				post = new PostDAO();

				post.setID(ID);
				post.setPostOwner(rs.getInt("post_owner"));
				post.setPostTopic(rs.getString("post_topic"));
				post.setPostDateTime(rs.getDate("post_dateTime"));
				post.setPostVoteCount(rs.getInt("post_voteCount"));
				post.setPostText(rs.getString("post_text"));
				post.setPostVideoId(rs.getInt("post_videoId"));
				post.setPostForumId(rs.getInt("post_forumId"));
			}

		} catch (SQLException e) {
			System.out.println("SQL Error in getPostWithID. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in getPostWithID. ");
			e.printStackTrace();
		} finally {

			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					System.out.println("Error in getPostWithID. ");
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("Error in getPostWithID. ");
					e.printStackTrace();
				}
			}
		}
		return post;
	}
	
	public static List<PostDAO> getPostList(int topicId) {
		List<PostDAO> postList = new ArrayList<PostDAO>();
		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return null;
		}

		PostDAO post = null;

		try {
			ps = ConnectionManager.conn
					.prepareStatement("select * from tbl_Post where post_forumId = ?");

			ps.setInt(1, topicId);

			rs = ps.executeQuery();

			while (rs.next()) {

				post = new PostDAO();

				post.setID(rs.getInt("ID"));
				post.setPostOwner(rs.getInt("post_owner"));
				post.setPostTopic(rs.getString("post_topic"));
				java.util.Date date  = rs.getTimestamp("post_dateTime"); 
				post.setPostDateTime(new Date(date.getTime()));
				post.setPostVoteCount(rs.getInt("post_voteCount"));
				post.setPostText(rs.getString("post_text"));
				post.setPostVideoId(rs.getInt("post_videoId"));
				post.setPostForumId(rs.getInt("post_forumId"));
				postList.add(post);
			}

		} catch (SQLException e) {
			System.out.println("SQL Error in getPostWithID. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in getPostWithID. ");
			e.printStackTrace();
		} finally {

			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					System.out.println("Error in getPostWithID. ");
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("Error in getPostWithID. ");
					e.printStackTrace();
				}
			}
		}
		return postList;
	}

	public static boolean addPost(PostDAO post) {

		boolean result = false;

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		PreparedStatement ps = null;
		try {
			ps = ConnectionManager.conn
					.prepareStatement(" insert into tbl_Post (post_owner, post_topic, post_dateTime, post_voteCount, post_text, post_videoId, post_forumId)"
							+ " values (?, ?, ?, ?, ?, ?, ?)");

			ps.setInt(1, post.getPostOwner());
			ps.setString(2, post.getPostTopic());
			Timestamp time = new Timestamp(post.getPostDateTime().getTime());
			ps.setTimestamp(3, time);
			ps.setInt(4, post.getPostVoteCount());
			ps.setString(5, post.getPostText());
			ps.setInt(6, post.getPostVideoId());
			ps.setInt(7, post.getPostForumId());

			ps.executeUpdate();

			ResultSet generatedKeys = ps.getGeneratedKeys();
			if (generatedKeys.next()) {
				post.setID(generatedKeys.getInt(1));
			} else {
				throw new SQLException("Creating post failed, no ID obtained.");
			}

			result = true;
		} catch (SQLException e) {
			System.out.println("SQL Error in addPost. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in addPost. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("Error in addPost. ");
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public boolean updatePost(PostDAO post) {

		boolean result = false;

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		PreparedStatement ps = null;
		try {
			PostDAO oldPost = getPostWithID(post.getID());

			if (oldPost == null) {
				System.out.println("No Post found with ID = " + post.getID());
				return result;
			}

			ps = ConnectionManager.conn
					.prepareStatement("update tbl_Post set post_owner = ?, post_topic = ?, post_dateTime = ?, post_voteCount = ?, post_text = ?, post_videoId = ?, post_forumId = ? where ID = ?");

			ps.setInt(1, post.getPostOwner());
			ps.setString(2, post.getPostTopic());
			ps.setDate(3, post.getPostDateTime());
			ps.setInt(4, post.getPostVoteCount());
			ps.setString(5, post.getPostText());
			ps.setInt(6, post.getPostVideoId());
			ps.setInt(7, post.getPostForumId());
			ps.setInt(8, post.getID());

			ps.executeUpdate();

			result = true;
		} catch (SQLException e) {
			System.out.println("SQL Error in updatePost. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in updatePost. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in addPost. ");
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static boolean deletePost(int ID) {
		boolean result = false;

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		try {
			PostDAO oldPost = getPostWithID(ID);

			if (oldPost == null) {
				System.out.println("No Post found with ID = " + ID);
				return result;
			}

			ps = ConnectionManager.conn
					.prepareStatement("delete from tbl_Post where ID = ?");

			ps.setInt(1, ID);

			ps.execute();

			result = true;

		} catch (SQLException e) {
			System.out.println("SQL Error in deletePost. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in deletePost. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("Error in deletePost. ");
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();

		sb.append("Post [ID:").append(ID);
		sb.append("; post_owner:").append(postOwner);
		sb.append("; post_topic:").append(postTopic);
		sb.append("; post_dateTime:").append(postDateTime);
		sb.append("; post_voteCount:").append(postVoteCount);
		sb.append("; post_text:").append(postText);
		sb.append("; post_videoId:").append(postVideoId);
		sb.append("; post_forumId:").append(postForumId);

		sb.append("]");

		return sb.toString();
	}
}
